package com.jvm123.excel.in.impl;

import com.jvm123.excel.common.ExcelProperties;
import com.jvm123.excel.in.MapConverter;
import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MapConverterImpl implements MapConverter {

    private static final Logger logger = LoggerFactory.getLogger(MapConverterImpl.class);

    private ExcelProperties excelProperties;
    private List<String> nameList;

    public MapConverterImpl() {
    }

    public MapConverterImpl(ExcelProperties excelProperties, List<String> nameList) {
        this.excelProperties = excelProperties;
        this.nameList = nameList;
    }

    @Override
    public List<Map<String, Object>> convertToMapList(Sheet sheet) {
        //用来存放表中数据
        List<Map<String, Object>> list = new ArrayList<>();
        if (sheet != null) {
            int rowNum = getRowNum(sheet);
            Row row;
            for (int i = excelProperties.getFirstDataRowIndex(); i < rowNum; i++) {
                Map<String, Object> map = new LinkedHashMap<>();
                row = sheet.getRow(i);
                if (row != null) {
                    for (int j = 0; j < nameList.size(); j++) {
                        Object cellData = getCellFormatValue(row.getCell(j));
                        map.put(nameList.get(j), cellData);
                    }
                } else {
                    break;
                }
                list.add(map);
            }
        }
        logger.debug("转换为mapList：{}", list);
        return list;
    }

    private int getRowNum(Sheet sheet) {
        int firstDataRowIndex = excelProperties.getFirstDataRowIndex();
        int lastDataRowIndex = excelProperties.getLastDataRowIndex();
        int dataRowNum = excelProperties.getDataRowNum();
        // 表格的最大行数
        int realRowNum = sheet.getPhysicalNumberOfRows();
        int rowNum = realRowNum;
        if (lastDataRowIndex > 0 && lastDataRowIndex < realRowNum) {
            rowNum = lastDataRowIndex;
        }
        if (dataRowNum > 0 && (firstDataRowIndex + dataRowNum) < realRowNum) {
            rowNum = firstDataRowIndex + dataRowNum;
        }
        return rowNum;
    }

    @Override
    public List<Map<String, Object>> convertToMapList(Workbook workbook) {
        Sheet sheet = workbook.getSheetAt(excelProperties.getSheetIndex());
        return convertToMapList(sheet);
    }

    private Object getCellFormatValue(Cell cell) {
        Object cellValue = null;
        if (cell != null) {
            CellType cellType = cell.getCellTypeEnum();
            // 判断cell类型
            switch (cellType) {
//                case _NONE:
//                    break;
                case NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {
                        // 日期时间
                        cellValue = cell.getDateCellValue();
                    } else {
                        // 数字
                        cellValue = cell.getNumericCellValue();
                    }
                    break;
                case STRING:
                    cellValue = cell.getStringCellValue();
//                    cellValue = cell.getRichStringCellValue().getString();
                    break;
                case FORMULA:
                    // 表达式
                    // 判断cell是否为日期格式
                    if (DateUtil.isCellDateFormatted(cell)) {
                        // 转换为日期格式YYYY-mm-dd
                        cellValue = cell.getDateCellValue();
                    } else {
                        // 数字
                        cellValue = cell.getNumericCellValue();
                    }
                    break;
//                case BLANK:
//                    break;
                case BOOLEAN:
                    cellValue = cell.getBooleanCellValue();
                    break;
//                case ERROR:
//                    break;
                default:
                    logger.warn("无法解析的单元格类型 {}", cellType);
                    break;
            }
        }
        return cellValue;
    }


    public ExcelProperties getExcelProperties() {
        return excelProperties;
    }

    public void setExcelProperties(ExcelProperties excelProperties) {
        this.excelProperties = excelProperties;
    }

    public List<String> getNameList() {
        return nameList;
    }

    public void setNameList(List<String> nameList) {
        this.nameList = nameList;
    }
}
